package groovyhelper;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;

import java.util.regex.Pattern;

public class GroovyCommentByBlockCommentAction extends GroovyBaseOverrideAction {
    void groovyActionPerformed(AnActionEvent anActionEvent, Editor editor, Project project, PsiFile psiFile) {
        SelectionModel selectionModel = editor.getSelectionModel();
        int start = selectionModel.getSelectionStart();
        int selectionEnd = selectionModel.getSelectionEnd();
        String text = editor.getDocument().getText();
        int startOfLine = text.lastIndexOf('\n', start) + 1;
        if (selectionEnd > startOfLine && text.charAt(selectionEnd) == '\n') {
            selectionEnd--;
        }
        int endOfLine = text.indexOf('\n',selectionEnd) + 1;

        if (endOfLine == 0) {
            endOfLine = text.length();
        }
        String selection = text.substring(startOfLine, endOfLine);
        selection = "/*\n" + selection + "*/\n";
        substitute(editor, startOfLine, endOfLine, selection, project, -3);
    }
}
